package com.sgb.jicai.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.luck.picture.lib.decoration.GridSpacingItemDecoration;
import com.luck.picture.lib.tools.ScreenUtils;
import com.sgb.jicai.R;
import com.sgb.jicai.databinding.FragmentQuotedPriceOfferBinding;
import com.sgb.jicai.entity.OfferGroupBuyingDetailsEntity;
import com.sgb.jicai.view.activity.JiCaiQuotedPriceActivity;
import com.sgb.jicai.view.adapter.ParticipationOfferListAdapter;
import com.sgb.jicai.weight.PriceRolePopWindow;
import com.sgb.link.view.adapter.SelfTeamPicAdapter;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.customview.PhotoViewActivity;
import com.swgk.core.customview.ViewPagerForScrollView;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 参与报价页面--报价Fragment
 */
public class QuotedPriceOfferFragment extends AppLazyFragment {

    FragmentQuotedPriceOfferBinding binding;

    private ParticipationOfferListAdapter participationOfferListAdapter;
    private OfferGroupBuyingDetailsEntity.GroupBuyingDetails currentGroupBuyingDetails;//当前被编辑价格规则的物料
    private int currentIndex;
    private PriceRolePopWindow popWindow;
    public SelfTeamPicAdapter mPicAdapter;
    //集采类型(1是即报即采, 2是汇集采购, 3是企业专项)
    private String groupBuyType;
    private int type; // 1.参与报价   2.修改报价

    private OfferGroupBuyingDetailsEntity detailEntity;

    private int pagerIndex;
    private ViewPagerForScrollView pager;
    JiCaiQuotedPriceActivity activity;
    /**
     * @param pager
     * @param pagerIndex
     * @param groupBuyType
     * @param type         // 1.参与报价   2.修改报价
     */
    public QuotedPriceOfferFragment(ViewPagerForScrollView pager, int pagerIndex, String groupBuyType, int type,JiCaiQuotedPriceActivity activity) {
        this.pager = pager;
        this.pagerIndex = pagerIndex;
        this.groupBuyType = groupBuyType;
        this.type = type;
        this.activity=activity;
    }

    public static QuotedPriceOfferFragment newInstance(ViewPagerForScrollView pager, int pagerIndex, String groupBuyType, int type , JiCaiQuotedPriceActivity activity) {
        return new QuotedPriceOfferFragment(pager, pagerIndex, groupBuyType, type,activity);
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_quoted_price_offer, container, false);
        pager.setObjectForPosition(binding.getRoot(), pagerIndex);
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        DividerItemDecoration divider = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.shape_list_divide_joinin_details_jc));
        if (null == participationOfferListAdapter) {
            participationOfferListAdapter = new ParticipationOfferListAdapter(getContext(), null, groupBuyType, type);
        }
        binding.recyclerJoin.addItemDecoration(divider);
        binding.recyclerJoin.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.recyclerJoin.setNestedScrollingEnabled(false);
        binding.recyclerJoin.setAdapter(participationOfferListAdapter);
        binding.recyclerJoin.setLoadingMoreEnabled(false);
     /*   if ("2".equals(groupBuyType)) {
            initPriceRolePop();
            participationOfferListAdapter.setItemCallBack(new ParticipationOfferListAdapter.ItemCallBack() {
                @Override
                public void goPriceRole(OfferGroupBuyingDetailsEntity.GroupBuyingDetails entity, TextView textView, int index) {
                    showPriceRolePop(entity, index);
                    currentIndex = index;
                }
            });
        }*/
        initPic();
    }

    @Override
    protected void onLazyLoad() {
        //因为列表数据时和其它数据一起返回的，其它数据activity中需展示，数据访问放在Activity中
    }

    /**
     * @param detailEntity
     */
    public void setDetailEntity(OfferGroupBuyingDetailsEntity detailEntity) {
        this.detailEntity = detailEntity;
        if (null!=detailEntity&&detailEntity.getGroupBuyingDetails().size()>0){
            for (OfferGroupBuyingDetailsEntity.GroupBuyingDetails groupBuyingDetail : detailEntity.getGroupBuyingDetails()) {
                groupBuyingDetail.setShoptype(detailEntity.getGroupBuyClass());
            }
        }
        if (null == detailEntity.getDeadline() || detailEntity.getDeadline().isEmpty()) {
            participationOfferListAdapter.setOfferEndtime(0);
        } else {
            participationOfferListAdapter.setOfferEndtime(Long.valueOf(detailEntity.getDeadline()));
        }
        if (null != detailEntity) {
            participationOfferListAdapter.setDatas(detailEntity.getGroupBuyingDetails());
        } else {
            participationOfferListAdapter.setDatas(null);
        }
        binding.leaseExpand.setEditContent(detailEntity.getOfferNotes());

    }

    public OfferGroupBuyingDetailsEntity getDetailEntity() {
        detailEntity.setOfferNotes(binding.leaseExpand.getEditContent());
        return detailEntity;
    }

    //初始化价格规则弹窗
    private void initPriceRolePop() {
        popWindow = new PriceRolePopWindow(getContext());
        popWindow.setBgView(binding.grayLayout);
        popWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        popWindow.setConfirmCallBack(new PriceRolePopWindow.ConfirmCallBack() {
            @Override
            public void getRolesData(List<OfferGroupBuyingDetailsEntity.GroupBuyingDetails> dataList) {
                //组装数据更新外层列表并刷新
                currentGroupBuyingDetails.setPriceRule(dataList);
                List<OfferGroupBuyingDetailsEntity.GroupBuyingDetails> groupBuyingDetails = detailEntity.getGroupBuyingDetails();
                groupBuyingDetails.set(currentIndex, currentGroupBuyingDetails);
                detailEntity.setGroupBuyingDetails(groupBuyingDetails);
                participationOfferListAdapter.setDatas(detailEntity.getGroupBuyingDetails());
            }
        });
    }

    /**
     *
     * @param entity 当前物料详情
     * @param index 当前物料id
     */
    public void showPriceRolePop(OfferGroupBuyingDetailsEntity.GroupBuyingDetails entity, int index) {
        if (null != popWindow) {
            currentGroupBuyingDetails = entity;
            if (2 == type) { // 物料报价数据列表，固定返回的是一条，所以此处固定取第一位
                entity.setPriceRule(entity.getDetails().get(0).getPriceRule());
            }else{
                entity.setPriceRule(new ArrayList<>());
            }
            popWindow.setDataList(null == entity.getPriceRule() ? new ArrayList<>() : entity.getPriceRule());
            popWindow.showDownPopwindow(binding.grayLayout, true);//llBottom    这块修改
        }
    }
    private void initPic() {
        GridLayoutManager manager = new GridLayoutManager(getContext(), 3);
        binding.rvPic.setLayoutManager(manager);
        mPicAdapter = new SelfTeamPicAdapter(getContext(), new ArrayList<>(), 1, manager);
        binding.rvPic.setHasFixedSize(false);
        binding.rvPic.addItemDecoration(new GridSpacingItemDecoration(4, ScreenUtils.dip2px(getContext(), 2), false));
        binding.rvPic.setAdapter(mPicAdapter);
        mPicAdapter.setOnItemClick(new AdapterOnItemClick<String>() {
            @Override
            public void onItemClick(String s, int position) {
                if (TextUtils.isEmpty(s)) {
                    activity.setPictureSelector();
                } else {
                    Intent intent = new Intent(getActivity(), PhotoViewActivity.class);
                    intent.putExtra(PhotoViewActivity.KEY_PHOTO_URLS, (Serializable) mPicAdapter.getDatas());
                    intent.putExtra(PhotoViewActivity.KEY_PHOTO_POSITION, position);
                    startActivity(intent);
                }
            }
        });
    }
}
